package com.yc.dict.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.dict.entity.DictDetail;
import com.yc.dict.service.IDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台字典详情表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-12-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/dictDetail")
@Api(value = "DictDetailController", tags = "后台字典详情表")
public class DictDetailController extends BaseController {

    private final IDictDetailService dictDetailService;

    @PostMapping("/addByDictId/{dictId}")
    @ApiOperation(value = "创建DictDetail", notes = "创建DictDetail")
    @SaCheckPermission(value = {"administrator", "management:dict:add"}, mode = SaMode.OR)
    public CommonResult<?> addByDictId(@PathVariable("dictId") Long dictId, @RequestBody DictDetail dictDetail) {
        dictDetail.setDictId(dictId);
        boolean isOk = dictDetailService.create(dictDetail);
        if (isOk) {
            return CommonResult.success(dictDetail);
        }
        return CommonResult.failed();
    }


    @PostMapping("/deleteByIds")
    @SaCheckPermission(value = {"administrator", "management:dict:delete"}, mode = SaMode.OR)
    @ApiOperation(value = "删除DictDetail", notes = "删除DictDetail")
    public CommonResult<?> deleteByIds(@RequestBody List<Long> ids) {
        boolean isOk = dictDetailService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @SaCheckPermission(value = {"administrator", "management:dict:update"}, mode = SaMode.OR)
    @ApiOperation(value = "更新DictDetail", notes = "更新DictDetail")
    public CommonResult<?> updateById(@PathVariable("id") long id, @RequestBody DictDetail dictDetail) {
        dictDetail.setId(id);
        boolean isOk = dictDetailService.update(dictDetail);
        if (isOk) {
            return CommonResult.success(dictDetail);
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取DictDetail", notes = "根据Id获取DictDetail")
    public CommonResult<?> queryById(@PathVariable("id") Long id) {
        DictDetail dictDetail = dictDetailService.queryById(id);
        if (dictDetail != null) {
            return CommonResult.success(dictDetail);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取DictDetail列表", notes = "获取DictDetail列表")
    public CommonResult<?> listAll() {
        List<DictDetail> list = dictDetailService.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取DictDetail列表", notes = "获取DictDetail列表")
    public CommonResult<?> pageAll(@RequestBody JSONObject params) {
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<DictDetail> page = dictDetailService.selectPage(new Page<>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }

}
